This is an example of migration to Unicode, using wide chars internally (UTF-16).
Two versions of TOOL are available :
Version using byte characters internally (not Unicode) :
-
The A functions are used.
-
The user functions called by name (like demons, actions) receive byte
char strings.
-
The function API_UseWideChar
returns GtFalse.
-
The macro TOOL_UNICODE is not defined.
Version using wide characters internally (Unicode UTF-16) :
-
At compile-time, the API of the Unicode version is available in two modes :
-
Mode byte char (when the macro TOOL_WIDE_API
is not defined) : The A functions are used.
-
Mode wide char (when the macro TOOL_WIDE_API
is defined) : The W functions are used.
-
The user functions called by name (like demons, actions) receive :
-
Byte char strings if their name ends with 'A'.
-
Wide char strings if their name ends with 'W'.
-
If their name doesn't end with 'A' or 'W' then the function
API_UserFnIsWide indicates which kind of string is passed : Byte
char if API_UserFnIsWide returns
GtFalse, or wide char if API_UserFnIsWide
returns GtTrue.
-
By default, API_UserFnIsWide returns :
-
GtFalse if TOOL has been initialized using API_InitA
or API_Init2A.
-
GtTrue if TOOL has been initialized using API_InitW
or API_Init2W.
-
You can change the value returned by API_UserFnIsWide
by calling API_UserFnSetWide.
-
In all cases, when a user function called by name receives
a size value which indicates the maximum size of a string buffer, this
size is expressed in characters count, *NOT* in byte count.
-
The function API_UseWideChar
returns GtTrue.
-
The macro TOOL_UNICODE is defined.
New API types :
typedef char
gtcharA;
typedef wchar_t gtcharW;
#ifdef TOOL_WIDE_API
#define gtchar gtcharW
#else
#define gtchar gtcharA
#endif
typedef gtchar * GtString;
typedef const gtchar * GtCString;
typedef gtcharA *GtStringA;
typedef const gtcharA *GtCStringA;
typedef gtcharW *GtStringW;
typedef const gtcharW *GtCStringW;
|
New API functions
-
GtBool API_StringA2W(GtCStringA pszA,
GtStringW *ppszW);
-
GtBool API_StringW2A(GtCStringW pszW,
GtStringA *ppszA);
-
GtBool API_SetCodePage(gtuint16 codepage);
-
gtuint16 API_GetCodePage();
-
GtBool API_UseWideChar();
-
GtBool API_UserFnIsWide();
-
GtBool API_UserFnSetWide(GtBool b);
|
New API macros :
| #define GTCHARCOUNT(X) (sizeof(X) /
sizeof(gtchar)) |
API changes :
-
The API_xxx functions that take char*
arguments are now correctly declared with the const keyword when applicable. As
a consequence, you may have 'conversion loses qualifier' errors when
compiling legacy code.
Conversion [multi-]byte <--> wide characters
Two conversion strategies are used in the implementation :
-
1st case : Conversion of strings passed through the byte char API.
The conversion is made using the TOOL functions API_StringA2W
and API_StringW2A. These functions call
respectively the system functions MultiByteToWideChar and
WideCharToMultiByte
which take a page code as argument.
-
2nd case : Conversion of strings read/written in text files.
Quoted from MSDN : When a Unicode stream-I/O function operates in text
mode, the source or destination stream is assumed to be a sequence of multibyte
characters. Therefore, the Unicode stream-input functions convert multibyte
characters to wide characters (as if by a call to the
mbtowc function). For the same reason, the Unicode stream-output
functions convert wide characters to multibyte characters (as if by a call to
the wctomb function). Conversions
between the two representations occur within the Standard C Library. The
conversion rules can, in principle, be altered by a call to
setlocale that alters the category LC_CTYPE.
Each wide stream determines its conversion rules at the time it becomes wide
oriented, and retains these rules even if the category
LC_CTYPE subsequently changes.
In both cases, the API_SetCodePage function
lets select the page code used by these conversion functions.
Callback management :
The following functions let register callbacks for various purposes. When linked
with the wide version of TOOL, both the A and W version can be called. If
you register both the byte and wide versions of a callback, then only
the wide version will be called by TOOL
-
API_SetHelpCallbackFn
-
API_SetTranslationFn
-
API_SetErrorFn
-
GtDocRegister.
-
GtDocSetLaunchEditorCallback
|
The following callbacks exported by gtdemons.dll are called by name. Only
the wide version of TOOL will check for the W version of those callbacks.
If both the A and W versions exist, then the W version is used. If neither the
A nor W version is found, and the neutral name is found, then
it's called as the A version.
-
GtdCpyWrtInfo
-
GtdDocInit
-
GtdInit
|
Note : The function API_UserFnSetWide has
no impact on the calling convention.
File formats
The binary files saved with the wide version of TOOL 4.x contain UTF-16
strings. A Byte Order Mark (BOM) is inserted at the begining of the file.
The binary files saved with the version 3.x or with the byte version can be read
by the wide version of TOOL.
| |
Text File .gtd .gtb without multibyte
characters
--> opened in "r" or "w" mode
|
Text file .gtd .gtb containing multibyte
characters
--> opened in "r" or "w" mode |
Binary file .gti .gte .gtm without BOM
--> opened in "rb" or "wb" mode |
Binary file .gti .gte .gtm with BOM
--> opened in "rb" or "wb" mode |
Version 3.x
Does not support Unicode.
|
-
Reading : ok
-
Writing : compatible with all versions.
|
-
Reading : will raise
syntax errors because the additional bytes will be handled as normal characters.
|
-
Reading : ok
-
Writing : compatible with all versions.
|
-
Reading : not loaded, no error message .
|
Version 4.x in byte char mode.
Does not support Unicode.
|
|
|
|
-
Reading : not loaded, error message : "Unicode format not
supported" .
|
Version 4.x in wide char mode.
Supports Unicode.
|
|
-
Reading : ok
-
Writing : not compatible with version 3.x and
version 4.x in byte mode.
|
-
Reading : ok, lets read the old files,
without conversion.
-
Writing : saved under UTF-16 format with BOM, even if it was
read from a file under byte char format without BOM.
Not compatible with version 3.x and version 4.x in byte mode.
|
-
Reading : ok
-
Writing : not
compatible with version 3.x and version 4.x in byte mode.
|